﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKRevitBase.Math;
using YJKRevitKernel.RevitTopologySuite;
using YJKRevitDimensionText.VerticalAutoDim.ProgressBar;
using YJKGridTopologyCal.Index.Quadtree;

namespace YJKRevitDimensionText.VerticalAutoDim.GeoAlgorithm
{
	internal class WindDoorGeoDepthClippedAlgorithm : IGeoDepthClippedAlgorithm
	{
		public IList<GeoAABB> ArrayGeoSubjectAABB
		{
			get
			{
				return this._arrayWallAABB;
			}
		}

		public WindDoorGeoDepthClippedAlgorithm(IList<GeoAABB> lstWallAABB, IDualStatusProgressBarProcess dualStatusProgressBarProcess)
		{
			this._arrayWallAABB = lstWallAABB;
			this.DualStatusProgressBarProcess = dualStatusProgressBarProcess;
		}

		public IList<GeoAABB> GeoDepthClipped(IList<GeoAABB> lstWindDoorGeoAABB)
		{
			List<GeoAABB> list = new List<GeoAABB>();
			Quadtree<GeoAABB> quadtree = new Quadtree<GeoAABB>();
			foreach (GeoAABB geoAABB in this.ArrayGeoSubjectAABB)
			{
				quadtree.Insert(geoAABB.AABB, geoAABB);
			}
			this.DualStatusProgressBarProcess.SetSubStatusMaxVal(lstWindDoorGeoAABB.Count);
			this.DualStatusProgressBarProcess.UpdatePrimaryStatus(3);
			int num = 0;
			foreach (GeoAABB geoAABB2 in lstWindDoorGeoAABB)
			{
				this.DualStatusProgressBarProcess.UpdateSubStatus(num++);
				ElementId id = (geoAABB2.IPlanarGeo.Element as FamilyInstance).Host.Id;
				List<GeoAABB> list2 = (from w in quadtree.Query(geoAABB2.AABB)
				orderby w.IPlanarGeo.Distance2Plane
				select w).ToList<GeoAABB>();
				for (int i = 0; i < list2.Count; i++)
				{
					GeoAABB geoAABB3 = list2[i];
					if (geoAABB2 != geoAABB3 && !(geoAABB3.IPlanarGeo.Element.Id == id))
					{
						if (MathUtility.LessThan(geoAABB2.IPlanarGeo.Distance2Plane, geoAABB3.IPlanarGeo.Distance2Plane))
						{
							break;
						}
                        geoAABB2.Intersect(geoAABB3, (YJKRevitBase.TopologySuite.BooleanOperatorKindEnum)2);
					}
				}
				double dArea = 0.0;
				EnumerableExternsion.ForEach<RevitPolygonDataBean>(geoAABB2.ArrayBoundaryPolygonClippedResult, delegate(RevitPolygonDataBean p)
				{
					if (p.Area != null)
					{
						dArea += p.Area.Value;
					}
				});
				if (MathUtility.GreaterThan(dArea, 1.0))
				{
					list.Add(geoAABB2);
				}
			}
			return list;
		}

		private readonly IList<GeoAABB> _arrayWallAABB;

		private readonly IDualStatusProgressBarProcess DualStatusProgressBarProcess;
	}
}
